//
//  CollectListTableViewController.m
//  FreeReading
//
//  Created by lanou3g on 15/11/7.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "CollectListTableViewController.h"
#import "EnjoyDetailViewController.h"

@interface CollectListTableViewController ()

@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation CollectListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.dataArray = [NSMutableArray arrayWithArray:[[CollectManager shareManager] selectDatabaseWithTag:self.tag]];
    if (self.dataArray.count > 0) {
        

    } else {
     
        UIImageView *collectImage = [[UIImageView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, kScreenHeight))];
        collectImage.image = [UIImage imageNamed:@"collect"];
        self.tableView.bounces = NO;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:collectImage];
    }
        
    [self navBar];
    
}

#pragma mark - 内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if ([self isViewLoaded] && self.view.window == nil) {
        
        self.view = nil;
    }
}


// 添加左navBar
- (void)navBar
{
    
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-arrowleft"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftBar:)];
    
    self.navigationItem.leftBarButtonItem = leftBar;
    
}

- (void)leftBar:(UIBarButtonItem *)left
{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *indetifier = @"collect";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indetifier];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:indetifier];
    }
    
    cell.textLabel.text = self.dataArray[indexPath.row][@"title"];
    
    
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [[CollectManager shareManager] deleteDatabaseWithTitle:self.dataArray[indexPath.row][@"title"]];
        [self.dataArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        if (self.dataArray.count > 0) {
            
            
        } else {
            
            UIImageView *collectImage = [[UIImageView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, kScreenHeight))];
            collectImage.image = [UIImage imageNamed:@"collect"];
            self.tableView.bounces = NO;
            self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
            [self.view addSubview:collectImage];
        }

    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    EnjoyDetailViewController *enjoyVC = [[EnjoyDetailViewController alloc] init];
    
    enjoyVC.url = self.dataArray[indexPath.row][@"url"];
    enjoyVC.pic_url = self.dataArray[indexPath.row][@"imageUrl"];
    enjoyVC.newsTitle = self.dataArray[indexPath.row][@"title"];
    enjoyVC.tag = self.dataArray[indexPath.row][@"tag"];
    [self.navigationController pushViewController:enjoyVC animated:YES];
    
}




@end
